function setUpButtons(){
	$('#PBChartsLink').bind('click', function(){
		window.location = 'charts';
	});
	
	$('#PBCommunityLink').bind('click', function(){
		window.location = 'community';
	});
	
	$('#PBMyResourcesLink').bind('click', function(){
		window.location = 'resources';
	});
	
	$('#controlArea').css({
		'margin-top': '143px',
		'height': $('#UserResultArea').height() 
	});
	
	// Display messages
	$('#userMessages').bind('click', function(){
		
		PicBoard.getTranslations(['lang_256_invitationMessage', 'lang_213_joinGroup', 'lang_257_ignoreMessage', 'lang_190_delete'], function(){});
		
		closeButton = $('<a>', { href: '#', 'style': 'float:right; margin-right:2px; margin-top:2px', 
			click: function(){
				$(this).parent().parent().remove();
			} 
		}).append(
			$('<img>', { src: '../images/cross-button.png', 'border': '0' })
		);
		
		text = $("<div>", { "style": "float:left; line-height:20px; margin-left: 5px" }).text("Messages");
		
		controls = $('<div>', { 'class': 'smallBold', 'style': 'border-bottom: 1px solid black; height:20px' }).append(text, closeButton);
		
		$(this).after(
			$('<div>', { id:'messagePopup' }).append(controls)
		);		

		if(PicBoard.messages.requests.length > 0){
			console.log(PicBoard.messages.requests);
			for(m in PicBoard.messages.requests){
				$('#messagePopup').append(
					$('<div>', { 'class': 'item' }).append(
						$('<a>', { 'href': 'managegroup?id=' + PicBoard.messages.requests[m].gID }).html( "<img src='../images/plus.png' border='0' /> New Group Request" )
					)
				);				
			}	
		}
		
		for(m in PicBoard.messages.messages){
			$('#messagePopup').append(
				$('<div>', { 'class': 'item' }).append(
					$('<a>', { 'href': '#', click: function(){ displayMessage(PicBoard.messages.messages[m]) } }).html( "<img src='../images/inbox-document-text.png' border='0' /> " + PicBoard.messages.messages[m].message.substring(0, 30) )
				)
			);
		}
		
	});
	
}

function displayMessage(message){

	var messageContainer = $("<div>", { id: 'messageContainer' });
	messageContainer.append($('<h1>', { html: 'Message viewer' }));
	messageContainer.append($('<p>', { html: message.message }));
	
	// If it's a group invitation, we need some accept / decline buttons.
	switch(message.type){
	
		case "Group Invitation":
			var controlHolder = $('<p>');
			controlHolder.append(PicBoard.languageMap.lang_266_invitationMessage);
			controlHolder.append($('<button>', { id:'acceptRequest', html: PicBoard.languageMap.lang_213_joinGroup }));
			controlHolder.append($('<button>', { id: 'rejectRequest', html: PicBoard.languageMap.lang_257_ignoreMessage }));
			
			messageContainer.append(controlHolder);
		break;
	
		case "GroupAcceptance":
			var controlHolder = $('<p>');
			controlHolder.append($('<button>', { id:'deleteMessage', html: PicBoard.languageMap.lang_190_delete }));
			
			messageContainer.append(controlHolder);		
		break;
	
	}
	
	jQuery.facebox(messageContainer);

	 $('button').button();
	 
	 $('#deleteMessage').bind('click', function(){
	 	$.post(PicBoard.baseURL + "rsrc.php?req=deleteMessage", { 'msgID': message.msgID }, function(data){
	 		if(data.statusCode == 200){
	 			window.location = "home";
	 		} else {
	 			alert(data.message);
	 		}
	 	}, 'json');	 	
	 });
	 
	 // Bind the buttons.
	 $('#acceptRequest').bind('click', function(){
	 	$.post(PicBoard.baseURL + "rsrc.php?req=acceptGroupInvitation", { 'gID':message.group_gID, 'msgID': message.msgID }, function(data){
	 		if(data.statusCode == 200){
	 			window.location = "home";
	 		} else {
	 			alert(data.message);
	 		}
	 	}, 'json');
	 });
	 
	$('#acceptRequest').bind('click', function(){
	 	$.post(PicBoard.baseURL + "rsrc.php?req=rejectGroupInvitation", { 'gID':message.group_gID, 'msgID': message.msgID }, function(data){
	 		if(data.statusCode == 200){
	 			window.location = "home";
	 		} else {
	 			alert(data.message);
	 		}
	 	}, 'json');
	 });
}

function parseMessages(){
	// Check to see if the user has any new messages.
	if(PicBoard.messages.unreadCount > 0){
		$('span#messageCount').text(PicBoard.messages.unreadCount);
		$('span#messageCount').css('display', 'block');
	}
}

// Queue setUpButtons() to run when the document is ready.
PicBoard.addLoadEvent( "setUpButtons()" );

// Queue parseMessages for when the initialisation data has been parsed.
PicBoard.addInitEvent("parseMessages()");